// pages/mine/mine.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    user: [],
    jobIndex: ''
  },


  // 跳转事件
  click: function (e) {
    var that = this;
    let idx = e.currentTarget.dataset.idx;
    let {
      user
    } = that.data;
    let enabled = that.data.user.enabled;
    console.log(enabled)
    let navUrl; //跳转链接

    app.confirmLogin(
      function () {
        console.log('idx==' + idx)
        switch (idx) {
          case "1": //我的活动
            navUrl = '/pages/activity/activity?actIdx=0';
            break;
          case "2": //代签到
            navUrl = '/pages/activity/activity?actIdx=1';
            break;
          case "3": //已完成
            navUrl = '/pages/activity/activity?actIdx=2';
            break;
          case "4": //我的产品
            navUrl = 'my-product/my-product';
            if (user.enabled <= 0) {
              that.check();
              return;
            }
            break;
          case "5": //我的名片
            navUrl = '/pages/my-card/my-card';
            break;
          case "6": //访客记录
            navUrl = 'visitor-record/visitor-record';
            break;
          case "7": //产品添加
            navUrl = '/pages/product-add/product-add';
            if (user.enabled <= 0) {
              that.check();
              return;
            }
            break;
          case "8": //我的好友
            navUrl = '/pages/news/my-friend/my-friend';
            break;
          case "10": //个人信息
            navUrl = 'mine-msg/mine-msg';
            break;
          case "11": //申请加入
            navUrl = '/pages/join/join';
            break;
          case "12": //官网编辑
            if (enabled <= 0) {
              wx.showModal({
                title: '提示',
                content: '此功能须信息认证后才能使用！',
                showCancel: true,
                confirmText: '认证',
                success: function (res) {
                  if (res.confirm) { //认证
                    wx.navigateTo({
                      url: '/pages/card-check/card-check',
                    })
                  }
                },
                fail: function (res) {},
                complete: function (res) {},
              })
              return;
            }
            navUrl = '/pages/home-add/home-add';
            break;
          case "13": //纸质材料
            navUrl = '/pages/images/images';
            break;
        }
        wx.navigateTo({
          url: navUrl,
        })
      }
    )
  },

  check() { //去认证
    wx.showModal({
      title: '提示',
      content: '此功能须信息认证后才能使用！',
      showCancel: true,
      confirmText: '认证',
      success: function (res) {
        if (res.confirm) { //认证
          wx.navigateTo({
            url: '/pages/card-check/card-check',
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  getPhoneNumber(e) { //获取手机号
    console.log(e)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var that = this;
    var encryptedData = encodeURIComponent(e.detail.encryptedData)
    var iv = encodeURIComponent(e.detail.iv)
    var params = {
      user_id: wx.getStorageSync('userId'),
      sessionkey: wx.getStorageSync('session_key'),
      encryptedData,
      iv,
    };
    app.request(app.globalData.host + 'common/getPhoneNumber', params, 'GET', true,
      function (res) {
        console.log(res)
        if (res.data.code == 1) {
          wx.showToast({
            title: '绑定成功！',
            duration: 1500,
          })
          that.setData({
            phoneNumber: res.data.data.phone,
            'user.phone': res.data.data.phone
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500,
          })
        }

      },
      function (err) {},
      function (complete) {})
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var isLogin = wx.getStorageSync('isLogin') || false;
    that.setData({
      isLogin
    })
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var user_id = wx.getStorageSync('userId')

    wx.request({
      url: app.globalData.host + 'member/getUserInfo',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        user_id: user_id
      },
      success: function (res) {
        console.log(res)
        console.log(res.data.data.user)
        wx.hideLoading();
        if (res.data.data.user) {
          that.setData({
            user: res.data.data.user || [],
            jobIndex: res.data.data.user.com_type,
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})